package cn.nfwx.tv.fragment.dialog;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.chan.net.HttpCallback;
import com.chan.net.Result;

import cn.nfwx.tv.R;
import cn.nfwx.tv.base.BaseDialogFragment;
import cn.nfwx.tv.helper.ValueCallback;
import cn.nfwx.tv.manager.UserManager;
import cn.nfwx.tv.okhttp.UrlHelper;
import cn.nfwx.tv.util.GlideUtil;
import cn.nfwx.tv.util.ScreenUtil;
import cn.nfwx.tv.util.ToastUtil;

public class SmsCheckDialogFragment extends BaseDialogFragment implements HttpCallback {

    EditText et_sms_check;

    String requestChkpicCommand;

    ValueCallback valueCallback;
    private String mobileNo;

    public static SmsCheckDialogFragment newInstance(String smsMobileNo) {
        SmsCheckDialogFragment instance = new SmsCheckDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putString("mobileNo", smsMobileNo);
        instance.setArguments(bundle);
        return instance;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_sms_check;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        Bundle bundle = getArguments();
        mobileNo = bundle.getString("mobileNo");
        et_sms_check = findView(R.id.et_sms_check);
        GlideUtil.loadImage(getContext(), UrlHelper.getSmsCheckImgUrl(mobileNo), (ImageView) findView(R.id.iv_sms_check));
        findView(R.id.bt_sms_check).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String chkpic = et_sms_check.getEditableText().toString();
                if (TextUtils.isEmpty(chkpic)) {
                    ToastUtil.show(getContext(), "验证码不能为空");
                    return;
                }
                if (chkpic.length() < 4) {
                    ToastUtil.show(getContext(), "请输入正确的验证码");
                    return;
                }
                requestChkpicCommand = UserManager.chkpic(SmsCheckDialogFragment.this.<Result<String>>bindToLifecycle(), mobileNo, chkpic, SmsCheckDialogFragment.this);
            }
        });

        findView(R.id.iv_sms_check).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                GlideUtil.loadImage(getContext(), UrlHelper.getSmsCheckImgUrl(mobileNo), (ImageView) findView(R.id.iv_sms_check));
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        valueCallback = null;
    }

    public void setValueCallback(ValueCallback valueCallback) {
        this.valueCallback = valueCallback;
    }

    /**
     * 是否填满整个屏幕
     */
    protected boolean isMatchParent() {
        return true;
    }

    @Override
    protected boolean isOutsideTouchable() {
        return true;
    }

    @Override
    protected boolean isTranslucentBackground() {
        return false;
    }

    @Override
    protected int getWidth() {
        return (int) (ScreenUtil.getDisplayWidth(getContext()) * 0.5);
    }

    @Override
    protected int getHeight() {
        return (int) (ScreenUtil.getDisplayHeight(getContext()) * 0.5);
    }

    @Override
    public void onSuccess(String requestCommand, Result result) {
        if (requestCommand.equals(requestChkpicCommand)) {
            if (valueCallback != null) {
                valueCallback.onReceiveValue("发送成功");
            }
            dismissAllowingStateLoss();
        }
    }

    @Override
    public void onFail(String requestCommand, Result result) {
        if (requestCommand.equals(requestChkpicCommand)) {
            ToastUtil.show(getContext(), result.getMessage());
            GlideUtil.loadImage(getContext(), UrlHelper.getSmsCheckImgUrl(mobileNo), (ImageView) findView(R.id.iv_sms_check));
        }
    }
}
